package com.zxbg.controller.proportalController;

import com.zxbg.controller.Code;
import com.zxbg.controller.Result;
import com.zxbg.domain.Employee;
import com.zxbg.service.proportalService.PIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/proportalindex")
public class PIndexController {
    @Autowired
    PIndexService PIndexService;

    @GetMapping("/getActiveNavMenu")
    public Result getActiveNavMenu(@RequestHeader String employee_id, @RequestHeader Integer tenant_id) {
        return new Result(Code.GET_OK, PIndexService.getActiveNavMenu(employee_id, tenant_id));
    }

    @GetMapping("/getActiveLunBoPic")
    public Result getActiveLunBoPic(@RequestHeader String employee_id, @RequestHeader Integer tenant_id) {
        return new Result(Code.GET_OK, PIndexService.getActiveLunBoPic(employee_id, tenant_id));
    }

    @PutMapping("/updateViewed/{id}")
    public Result updateViewed(@RequestHeader String employee_id, @PathVariable Integer id) {
        return new Result(Code.UPDATE_OK, PIndexService.updateViewed(employee_id, id));
    }

    @GetMapping("/getActiveNews")
    public Result getActiveNews(@RequestHeader String employee_id, @RequestHeader Integer tenant_id) {
        return new Result(Code.GET_OK, PIndexService.getActiveNews(employee_id, tenant_id));
    }

    @GetMapping("/getActiveNotices")
    public Result getActiveNotices(@RequestHeader String employee_id, @RequestHeader Integer tenant_id) {
        return new Result(Code.GET_OK, PIndexService.getActiveNotices(employee_id, tenant_id));
    }

    @GetMapping("/getActiveRegulation")
    public Result getActiveRegulation(@RequestHeader String employee_id, @RequestHeader Integer tenant_id) {
        return new Result(Code.GET_OK, PIndexService.getActiveRegulation(employee_id, tenant_id));
    }

    @PutMapping("/updateRecentUse/{app_id}")
    public Result updateRecentUse(@PathVariable String app_id, @RequestHeader String employee_id, @RequestHeader Integer tenant_id) {
        return new Result(Code.UPDATE_OK, PIndexService.updateRecentUse(employee_id, app_id, tenant_id));
    }

    @PutMapping("/updateSelected")
    public Result updateSelected(@RequestBody Map<String, String> map,  @RequestHeader String employee_id, @RequestHeader Integer tenant_id) {
        Employee employee = PIndexService.updateSelected(map.get("app_id"), Integer.parseInt(map.get("mode")), employee_id, tenant_id);
        boolean ifSuccess = employee != null;
        return new Result(ifSuccess ? Code.UPDATE_OK : Code.UPDATE_ERR, employee, ifSuccess ? "success" : "最多可收藏5条，请先移除一部分收藏后再添加新的收藏" );
    }
}
